import 'package:flutter/material.dart';

class SearchBarWidget extends StatefulWidget {
  // 提示文本
  String hintText;

  // 搜索内容变化时的回调
  final Function(String)? onSearchChanged;

  // 搜索提交时的回调
  Function(String)? onSearchSubmitted;

  // 清除搜索内容时的回调
  VoidCallback? onClear;

  //搜索栏高度
  double height = 45;

  SearchBarWidget({
    super.key,
    this.hintText = '请输入搜索内容',
    this.onClear,
    this.onSearchChanged,
    this.onSearchSubmitted,
  });

  @override
  State<SearchBarWidget> createState() => _SearchBarWidgetState();
}

class _SearchBarWidgetState extends State<SearchBarWidget> {
  final TextEditingController _searchController = TextEditingController();
  bool showClearBtn = false;
  static List<String> supportCitys = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _searchController.addListener(_clearBtnVisibal);
  }

  @override
  void dispose() {
    _searchController.dispose();
    super.dispose();
  }

  // 清除搜索内容
  void _clearSearch() {
    _searchController.clear();
    widget.onSearchChanged?.call('');
    if (widget.onClear != null) {
      widget.onClear!();
    }
  }

  @override
  Widget build(BuildContext context) {
    return searchBar();
  }

  searchBar() {
    return Container(
      // 1. 使用Container设置固定高度
      height: widget.height,
      padding: const EdgeInsets.symmetric(horizontal: 8.0), // 可选：调整水平内边距
      child: TextField(
        controller: _searchController,
        decoration: InputDecoration(
          hintText: widget.hintText,
          prefixIcon: IconButton(
            onPressed: _searchContent,
            icon: Icon(Icons.search, color: Colors.blue),
          ),
          suffixIcon: showClearBtn
              ? IconButton(
                  icon: const Icon(Icons.clear, color: Colors.grey),
                  onPressed: _clearSearch,
                )
              : null,
          border: const OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(12.0)),
            borderSide: BorderSide(color: Colors.grey),
          ),
          focusedBorder: const OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(12.0)),
            borderSide: BorderSide(color: Colors.grey),
          ),
          // 2. 调整内容内边距，配合高度使用
          contentPadding: const EdgeInsets.symmetric(vertical: 12.0),
          isDense: false, // 设为false，允许更大的内边距
          filled: true,
          fillColor: Colors.white60,
        ),
        textInputAction: TextInputAction.search,
        onChanged: widget.onSearchChanged,
        onSubmitted: widget.onSearchChanged,
      ),
    );
  }

  searchText() {
    return Padding(
      padding: EdgeInsets.only(left: 6),
      child: GestureDetector(
        onTap: _searchContent,
        child: Text(
          style: TextStyle(
            color: Colors.blue,
            fontSize: 20,
            fontWeight: FontWeight.w300,
          ),
          "搜索",
        ),
      ),
    );
  }

  static List<String> _searchContent() {
    //todo 将搜索到的内容返回到外边
    return supportCitys;
  }

  void _clearBtnVisibal() {
    final Empty = _searchController.text.isEmpty;
    if (Empty) {
      setState(() {
        showClearBtn = false;
      });
    } else {
      setState(() {
        showClearBtn = true;
      });
    }
  }
}
